package com.tyrant.macaulottery.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.tyrant.macaulottery.HomeFragment;
import com.tyrant.macaulottery.MessageFragment;
import com.tyrant.macaulottery.PreferentialFragment;
import com.tyrant.macaulottery.R;
import com.tyrant.macaulottery.RegisterFragment;
import com.tyrant.macaulottery.utils.BrowserHelper;
import com.tyrant.macaulottery.utils.SharePreferencesUtil;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String[] REQUIRED_PERMISSIONS = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET
    };

    private ArrayList<Fragment> fragmentArrayList = new ArrayList<Fragment>();

    private FragmentManager fm;
    private Fragment currentFragment;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    switchContent(fragmentArrayList.get(0));
//                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:

                    switchContent(fragmentArrayList.get(1));
//                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:

                    switchContent(fragmentArrayList.get(2));
//                    mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.navigation_register:
                    String registerWebHost = SharePreferencesUtil.getSpString("registerWebHost", "http://cqshangji.com", MainActivity.this);
                    BrowserHelper.goBrowser(MainActivity.this, registerWebHost);


//                    switchContent(fragmentArrayList.get(3));
//                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_bar_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setLogo(R.drawable.push);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

//        RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance
//        rxPermissions
//                .request(REQUIRED_PERMISSIONS)
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(@io.reactivex.annotations.NonNull Boolean granted) throws Exception {
//                        Toast.makeText(CalculatorActivity.this, "granted:" + granted, Toast.LENGTH_SHORT).show();
//                        if (granted) {
//                            // All requested permissions are granted
//                        } else {
//                            // At least one permission is denied
//                        }
//                    }
//                });
        fragmentArrayList.add(new HomeFragment());
        fragmentArrayList.add(new PreferentialFragment());
        fragmentArrayList.add(new MessageFragment());
        fragmentArrayList.add(new RegisterFragment());
        showFragmentIndex();
    }

    private void showFragmentIndex() {
        fm = getSupportFragmentManager();
        currentFragment = fragmentArrayList.get(0);
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, fragmentArrayList.get(0));
        ft.commit();
    }

    public void switchContent(Fragment to) {
        if (currentFragment != to) {
//            FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(
//                    android.R.anim.fade_in, R.anim.slide_out_to_bottom);
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(currentFragment).add(R.id.content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
            currentFragment = to;
        }
    }

}
